package com.lntea.security.md5;

import java.security.MessageDigest;
import java.security.Security;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

public class LnteaMD5 {
	private static String message = "lntea md5";
	public static void main(String[] args) {
		jdkMD5();
		
		bcMD5();
		
		ccMD5();
	}
	
	public static void jdkMD5(){
		try {
			MessageDigest md = MessageDigest.getInstance("md5");
			byte[] digestBytes = md.digest(message.getBytes());
			System.out.println("jdk md5:"+new String(Base64.encodeBase64(digestBytes)));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void bcMD5(){
		Security.addProvider(new BouncyCastleProvider());
		
		try {
			MessageDigest md = MessageDigest.getInstance("md4");
			byte[] digestBytes = md.digest(message.getBytes());
			System.out.println("bc md5:"+new String(Base64.encodeBase64(digestBytes)));
			System.out.println("bc md5 hex:"+new String(Hex.encode(digestBytes)));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void ccMD5(){
		byte[] digestBytes = DigestUtils.md5(message.getBytes());
		System.out.println("cc md5:"+ new String(Base64.encodeBase64(digestBytes)));
		
		String digestStr = DigestUtils.md5Hex(message.getBytes());
		System.out.println("cc md5 hex:"+digestStr);
	}
}
